package com.yfp.client.controller;

import cn.hutool.core.util.BooleanUtil;
import com.yfp.client.annotation.AnonymousAccess;
import com.yfp.client.domain.EnterpriseOrder;
import com.yfp.client.domain.OrderCombo;
import com.yfp.client.domain.dto.*;
import com.yfp.client.domain.vo.DetailVO;
import com.yfp.client.domain.vo.OrderCreateVO;
import com.yfp.client.domain.vo.OrderVO;
import com.yfp.client.service.EnterpriseOrderService;
import com.yfp.client.service.OrderService;
import com.yfp.client.util.Response;
import com.yfp.client.util.ResponseEnum;
import com.yfp.client.util.SecurityUtils;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.client.domain.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 订单
 *
 *
 * @version 1.0
 * @date 2024-05-24 10:20
 */
@RestController
@RequestMapping("/client/order")
@Tag(name = "订单模块")
public class OrderController extends BaseController {
    @Resource
    private OrderService orderService;
    @Resource
    private EnterpriseOrderService enterpriseOrderService;

    @GetMapping("/applet/limit/status")
    @Operation(summary = "用户获取订单列表", description = "小程序订单列表页面使用")
    public Response<TableDataInfo> getAllOrder(OrderListDTO orderListDTO) {
        String userId = SecurityUtils.getClientId();
        orderListDTO.setUserId(userId);
        startPage();
        List<OrderCombo> orderCombos = orderService.listByUserIdAndStatus(orderListDTO);
        TableDataInfo tableDataInfo = getDataTable(orderCombos);
        List<OrderCombo> rows = tableDataInfo.getRows();
        List<OrderVO> allOrder = orderService.getAllOrder(rows, orderListDTO);
        tableDataInfo.setRows(allOrder);
        return Response.success(tableDataInfo);
    }

    @GetMapping("/detail")
    @Operation(summary = "订单详情", description = "小程序详情页面使用")
    public Response<DetailVO> detail(@RequestParam(value = "orderId") String orderId, @RequestParam(value = "latitude") String latitude, @RequestParam(value = "longitude") String longitude) {
        return Response.success(orderService.detail(orderId, latitude, longitude));
    }

    @GetMapping("/status/remind")
    @Operation(summary = "订单状态提醒", description = "小程序首页页面使用")
    public Response<DetailVO> remindOrderStatus() {
        return Response.success(orderService.remindOrderStatus());
    }

    @AnonymousAccess
    @PostMapping("/settlement/info")
    @Operation(summary = "订单结算", description = "小程序创建订单使用-订单结算-1先订单结算")
    public Response<ClientCouponBestDTO> settlementInfoOrder(@RequestBody OrderCreateDTO orderCreateDTO) {
        if (Objects.nonNull(orderCreateDTO.getClientCouponId()) && -1 == orderCreateDTO.getClientCouponId()) {
            orderCreateDTO.setClientCouponId(null);
        }
        ClientCouponBestDTO clientCouponBestDTO = orderService.settlementInfoOrder(orderCreateDTO);
        return Response.success(clientCouponBestDTO);
    }

    @GetMapping("/awaitNum")
    @Operation(summary = "等待数量")
    public Response<Integer> getAwaitNum(@NotNull String machineId) {
        return Response.success(orderService.getAwaitNum(machineId));
    }

    @PostMapping("/create")
    @Operation(summary = "创建订单-去支付", description = "小程序创建订单使用-去支付-2然后订单创建")
    public Response<OrderCreateVO> createOrder(@RequestBody OrderCreateDTO orderCreateDTO) {
        if (Objects.nonNull(orderCreateDTO.getClientCouponId()) && -1 == orderCreateDTO.getClientCouponId()) {
            orderCreateDTO.setClientCouponId(null);
        }
        OrderCreateVO order = orderService.createOrder(orderCreateDTO);
        // 是否是霸王免单券
        Boolean isBabyCouponOrder = orderService.isBabyCouponOrder(order.getCouponId());
        if (isBabyCouponOrder) {
            // 是否是霸王免单券，如果是直接跳过支付
            order = orderService.simulateWechatPay(order);
        }
        return Response.success(order);
    }

    @GetMapping("/cancel")
    @Operation(summary = "取消订单", description = "小程序取消订单使用")
    public Response<Boolean> cancelOrder(@RequestParam String orderId, @RequestParam String refundReason) {
        return Response.success(orderService.cancelOrder(orderId, refundReason));
    }

    @GetMapping("/refund")
    @Operation(summary = "订单退款", description = "小程序详情页面使用")
    public Response<Boolean> refundOrder(@RequestParam String orderId, @RequestParam String refundReason) {
        return Response.success(orderService.refundOrder(orderId, refundReason));
    }

    @PostMapping("/detail/QRCode")
    @Operation(summary = "获取二维码", description = "小程序详情页面使用")
    public Response<String> getQRCode(@RequestBody OrderDetailBtnDTO orderDetailBtnDTO) {
        return Response.success(orderService.getQRCode(orderDetailBtnDTO));
    }

    @PostMapping("/detail/operation/pickup")
    @Operation(summary = "客服代取", description = "小程序详情页面使用")
    public Response<Boolean> operationPickup(@RequestBody OrderDetailBtnDTO orderDetailBtnDTO) {
        return Response.success(orderService.operationPickup(orderDetailBtnDTO));
    }

    @PostMapping("/detail/action/package/make")
    @Operation(summary = "立即制作套餐（存柜）", description = "小程序详情页面使用")
    public Response<?> makePackage(@RequestBody OrderDetailBtnDTO orderDetailBtnDTO) {
        return orderService.makePackage(orderDetailBtnDTO);
    }

    @PostMapping("/enterprise")
    @Operation(summary = "企业二维码领取订单", description = "企业二维码领取订单")
    public Response<?> enterprise(@RequestBody EnterpriseOrderClientDTO enterpriseOrderClientDTO) {
        ResponseEnum responseEnum = enterpriseOrderService.enterpriseOrder(enterpriseOrderClientDTO);
        return Response.result(responseEnum);
    }

    @GetMapping("/getEnterpriseOrder/{orderSn}")
    @Operation(summary = "获取企业二维码订单", description = "获取企业二维码订单")
    public Response<EnterpriseOrder> getEnterpriseOrder(@PathVariable("orderSn") String orderSn) {
        EnterpriseOrder enterpriseOrderVO = enterpriseOrderService.findByOrderSn(orderSn);
        if (null == enterpriseOrderVO) {
            return Response.result(ResponseEnum.ENTERPRISEORDER_OUT_TIME);
        }

        String enterpriseName = enterpriseOrderService.findEnterpriseNameByEnterpriseId(enterpriseOrderVO.getEnterpriseId());
        enterpriseOrderVO.setEnterpriseName(enterpriseName);
        return Response.success(enterpriseOrderVO);
    }

    @GetMapping("/guidance/popup")
    @Operation(summary = "下单后引导弹窗", description = "小程序订单详情页面使用，下单前三次弹窗提示")
    public Response<Boolean> guidancePopup() {
        return Response.success(orderService.guidancePopup());
    }

    @GetMapping("/isFirstOrder")
    @Operation(summary = "是否是第一次下单", description = "小程序发送优惠券到账通知使用，是否是第一次下单")
    public Response<Integer> isFirstOrder() {
        Boolean firstOrder = orderService.isFirstOrder();
        return Response.success(BooleanUtil.toInteger(firstOrder));
    }
}
